﻿using COSXML;
using COSXML.Auth;
using Microsoft.Extensions.DependencyInjection;
using TencentCloud.Common;
using TencentCloud.Sms.V20190711;

namespace Simple.WebAi.Extensions
{
    /// <summary>
    /// 阿里云服务注册
    /// </summary>
    public static class TencentCloudServiceExtensions
    {
        /// <summary>
        /// 框架扩展：注册使用服务
        /// </summary>
        /// <param name="services"></param>
        /// <returns></returns>
        public static void AddTencentCloudServices(this IServiceCollection services)
        {
            var configuration = services.BuildServiceProvider().GetService<IConfiguration>()!;
            var secretId = configuration!["TencentCloud:SecretId"]!;
            var secretKey = configuration!["TencentCloud:SecretKey"]!;

            // 短信发送
            Credential cred = new Credential
            {
                SecretId = secretId,
                SecretKey = secretKey
            };
            SmsClient client = new SmsClient(cred, "ap-guangzhou");
            services.AddSingleton<SmsClient>(client);

            // 对象存储
            string region = configuration!["TencentCloud:CosRegion"]!;
            CosXmlConfig config = new CosXmlConfig.Builder()
                .SetRegion(region) // 设置默认的地域, COS 地域的简称请参照 https://cloud.tencent.com/document/product/436/6224
                .Build();
            long durationSecond = 600; //每次请求签名有效时长，单位为秒
            QCloudCredentialProvider qCloudCredentialProvider = new DefaultQCloudCredentialProvider(secretId, secretKey, durationSecond);
            var cosXml = new CosXmlServer(config, qCloudCredentialProvider);
            services.AddSingleton<CosXmlServer>(cosXml);
        }
    }
}
